package com.lanqiao.gxnushop.service;

import com.lanqiao.gxnushop.entity.Products;
import com.lanqiao.gxnushop.mapper.ProductsMapper;
import com.lanqiao.gxnushop.util.UUIdUtil;
import com.lanqiao.gxnushop.valueObject.ProductsF;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class ProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    /**添加商品*/
    @Cacheable(value = "productsCache", key = "#products.productId")
    public boolean add(Products products){
        products.setProductId(UUIdUtil.getUserId());
        return productsMapper.add(products);
    }

    /**删除商品*/
    @CacheEvict(value = "productsCache", key = "#productId")
    public boolean delete(String productId){
        return productsMapper.delete(productId);
    }

    /**根据商品id查询商品*/
    @Cacheable(value = "productsCache", key = "#productId")
    public Products findById(String productId){
        return productsMapper.findById(productId);
    }

    /**根据商品信息搜索商品列表*/
/*    public ArrayList<Products> findByProduct(Products products){
        return productsMapper.findByProduct(products);
    }*/
    /**更新商品*/
    @CachePut(value = "productsCache", key = "#products.productId")
    public boolean update(Products products){
        return productsMapper.update(products);
    }

    /**更新商品库存*/
    @CachePut(value = "productsCache", key = "#productId")
    public boolean updateStock(String productId,String stock){
        return productsMapper.updateStock(productId,stock);
    }

    /**查看自己 店铺 下的商品*/
    public ArrayList<Products> findByStoreId(Products products){
        return productsMapper.findByStoreId(products);
    }

    /**查看所有商品，用于首页商品展示*/
    @Cacheable(value = "productsCache", key = "'allProducts'")
    public ArrayList<Products> list(){
        return productsMapper.list();
    }

    /**通过商品名字模糊查看商品，用于首页    用户搜索商品  */
    @Cacheable(value = "productsCache", key = "#name + '-name'")
    public ArrayList<Products> selectByName(String name){
        return productsMapper.selectByName(name);
    }

    /**通过商品名字模糊查看商品，用于首页    用户搜索商品  hsz state = 1*/
    @Cacheable(value = "productsCache", key = "#name + '-name2'")
    public ArrayList<Products> selectByName2(String name){
        return productsMapper.selectByName2(name);
    }

    /**通过商品分类查看商品，用户搜索商品  */
    @Cacheable(value = "productsCache", key = "#name + '-category'")
    public ArrayList<Products> selectByGoodsCategory(String name){
        return productsMapper.selectByGoodsCategory(name);
    }

    /**商家库存管理*/
    public ArrayList<Products> getStock(ProductsF productsF){
        return productsMapper.getStock(productsF);
    }

    /**更新商品图片*/
    @CachePut(value = "productsCache", key = "#products.productId")
    public boolean updateImgUrl(Products products) {
        return productsMapper.updateImgUrl(products);
    }

}
